#!/usr/bin/env python
# -*- coding:utf-8 -*-


from live_util.common.censor_words import CensorWords
from dm_py_util import DEBUG_LOG, ERROR_LOG, INFO_LOG, WARN_LOG
from tornado import gen, escape
import time


def check_str(pinyin, nick):
    if not nick or len(nick) == 0:
        return False

    zuan_flag = 0
    yuan_flag = 0
    num_flag = 0
    num_pinyin_flag = 0
    q_flag = 0
    special_flag = 0
    touxiang_flag = 0
    ziliao_flag = 0
    vx_flag = 0
    renqi_flag = 0
    renqi_u_flag = 0
    daichong_flag = 0
    censor_yuan_set = (['$', '￥'])
    censor_num_set = (['❶', '❷', '❸', '❹', '❺', '❻', '❼', '❽', '❾', '❿',
                       '①', '②', '③', '④', '⑤', '⑥', '⑦', '⑧', '⑨', '⑩',
                       '０', '１', '２', '３', '４', '５', '６', '７', '８', '９',
                       '⒈', '⒉', '⒊', '⒋', '⒌', '⒍', '⒎', '⒏', '⒐',
                       '⑴', '⑵', '⑶', '⑷', '⑸', '⑹', '⑺', '⑻', '⑼',
                       '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
                       '⓪', '➕', 'l', 'o', 'O', 'Ⅰ', 'Ⅱ',
                       '1⃣', '2⃣', '3⃣', '4⃣', '5⃣', '6⃣', '7⃣', '8⃣', '9⃣', '0⃣'])
    censor_zuan_pinyin_set = (['zuan', 'zhuan', 'chan'])
    censor_num_pinyin_set = (
        ['yi', 'er', 'san', 'si', 'wu', 'liu', 'qi', 'ba', 'jiu'])
    censor_yuan_pinyin_set = (['yuan', 'kuai'])
    censor_special_pinyin_set = (['wan', 'kan'])
    censor_touxiang_pinyin_set = (['tou', 'xiang'])
    censor_ziliao_pinyin_set = (['zi', 'liao'])
    censor_vx_set = (['v', 'x', 'V', 'X', 'w'])
    censor_renqi_u_set = (['shua', 'shang'])
    censor_renqi_set = (['ren', 'qi', 're', 'men', 'fen', 'si'])
    censor_daichong_set = (['dai', 'chong'])
    censor_q_set = (['q', 'Q'])

    if ('💍' in nick) or ('💎' in nick):
        zuan_flag += 1
    if '💰' in nick:
        yuan_flag += 1
    if isinstance(nick, str):
        try:
            unicode_nick = escape.to_unicode(nick)
        except Exception as e:
            ERROR_LOG('_check_censor_nick to_unicode error', e)
            return False
    elif isinstance(nick, unicode):
        unicode_nick = nick
    if not unicode_nick or len(unicode_nick) == 0:
        return False
    for unicode_word in unicode_nick:
        try:
            utf8_word = escape.utf8(unicode_word)
        except Exception as e:
            ERROR_LOG('_check_censor_nick to_utf8 error', e)
            return False

        if utf8_word in censor_yuan_set:
            yuan_flag += 1
        elif utf8_word in censor_num_set:
            num_flag += 1
        elif utf8_word in censor_q_set:
            q_flag += 1
        elif utf8_word in censor_vx_set:
            if censor_vx_set.index(utf8_word) == 0 or censor_vx_set.index(utf8_word) == 2 \
                    or censor_vx_set.index(utf8_word) == 4:
                vx_flag = vx_flag | 1
            else:
                vx_flag = vx_flag | 2
        else:
            if not pinyin:
                continue
            pinyin_word = _convert_pinyin(pinyin, unicode_word)
            if not pinyin_word:
                continue
            if pinyin_word in censor_zuan_pinyin_set:
                zuan_flag += 1
            elif pinyin_word in censor_yuan_pinyin_set:
                yuan_flag += 1
            elif pinyin_word in censor_num_pinyin_set:
                num_pinyin_flag += 1
                if pinyin_word in censor_renqi_set:
                    renqi_flag = renqi_flag | (
                        2 ** censor_renqi_set.index(pinyin_word))
            elif pinyin_word in censor_touxiang_pinyin_set:
                if censor_touxiang_pinyin_set.index(pinyin_word) == 0:
                    touxiang_flag = touxiang_flag | 1
                elif censor_touxiang_pinyin_set.index(pinyin_word) == 1:
                    touxiang_flag = touxiang_flag | 2
            elif pinyin_word in censor_ziliao_pinyin_set:
                if censor_ziliao_pinyin_set.index(pinyin_word) == 0:
                    ziliao_flag = ziliao_flag | 1
                elif censor_ziliao_pinyin_set.index(pinyin_word) == 1:
                    ziliao_flag = ziliao_flag | 2
            elif pinyin_word in censor_renqi_set:
                renqi_flag = renqi_flag | (
                    2 ** censor_renqi_set.index(pinyin_word))
            elif pinyin_word in censor_renqi_u_set:
                renqi_u_flag += 1
            elif pinyin_word in censor_special_pinyin_set:
                special_flag += 1
            elif pinyin_word in censor_daichong_set:
                daichong_flag = daichong_flag | (
                    2 ** censor_daichong_set.index(pinyin_word))
        if renqi_flag == 3 or renqi_flag == 12 or renqi_flag == 48:
            if renqi_u_flag > 0 and (vx_flag == 3 or q_flag >= 1):
                return True
            elif renqi_u_flag > 0 and vx_flag == 1:
                return True
        if renqi_flag == 15 or renqi_flag == 60 or renqi_flag == 51:
            if vx_flag > 0 or q_flag > 0:
                return True
        if renqi_flag == 63:
            return True
        if (ziliao_flag == 3 and special_flag >= 1) or (ziliao_flag == 3 and zuan_flag >= 1):
            return True
        if (touxiang_flag == 3 and special_flag >= 1) or (touxiang_flag == 3 and zuan_flag >= 1):
            return True
        if zuan_flag >= 1 and special_flag >= 2:
            return True
        if zuan_flag >= 1 and yuan_flag >= 1:
            return True
        if num_flag >= 6 or num_pinyin_flag >= 6:
            if zuan_flag >= 1 or yuan_flag >= 1 or q_flag >= 1:
                return True
    return False


def _convert_pinyin(pinyin, word):
    try:
        word = pinyin.get_pinyin(
            escape.to_unicode(word), '', False, True).encode('utf-8')
    except Exception as e:
        ERROR_LOG('_convert_pinyin error', e)
        return None
    return word

if __name__ == '__main__':
    CensorWords().init('/a8root/data/censorwords/censorwords.txt.business')
    censor = CensorWords()
    pinyin = censor.pinyin
    starttime = time.time()
    isads = check_str(
        pinyin, '映客刷人气500-10000人气，助你上热门，淘宝搜索：【映客套餐】或关注VX:【zzc8752】')
    usetime = time.time() - starttime
    print isads, usetime
